package com.situ.staff.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.staff.bean.Staff;
import com.situ.staff.service.StaffService;

@Controller
@RequestMapping("/staff")
public class StaffController {

	@Autowired
	private StaffService staffService;

	@GetMapping("/add")
	public String add() {
		return "staff/add";
	}

	@PostMapping("/add")
	public String getAdd(Staff staff, Model model) {

		int result = staffService.add(staff);
		if (result > 0) {
			model.addAttribute("erroe", "添加成功！！");
			List<Staff> staffs = staffService.getAll();
			model.addAttribute("staffs", staffs);
			return "staff/list";
		} else {
			model.addAttribute("erroe", "添加失败！！");
			return "staff/add";
		}
	}

	@GetMapping("/editById")
	public String getEdit(int id, Model model) {
		Staff staff = staffService.getById(id);
		model.addAttribute("staff", staff);
		return "staff/edit";

	}

	@RequestMapping("/edit")
	public String edits(Staff staff, Model model) {
		staffService.update(staff);
		List<Staff> staffs = staffService.getAll();
		model.addAttribute("staffs", staffs);
		return "staff/list";
	}

	@RequestMapping("/remove")
	public String getRemove(Model model, Integer id) {
		staffService.remove(id);
		List<Staff> staffs = staffService.getAll();
		model.addAttribute("staffs", staffs);
		return "staff/list";

	}

	@RequestMapping("/list")
	public String list(Model model) {

		List<Staff> staffs = staffService.getAll();
		model.addAttribute("staffs", staffs);
		return "staff/list";
	}

	@PostMapping("/getAll")
	@ResponseBody
	public List getAll() {
		List staffs = staffService.getAll();
		return staffs;

	}

	@PostMapping("/getByDeptId")
	@ResponseBody
	public List getByDeptId(@RequestParam("dept_id") Integer dept_id) {
		List staffs = staffService.getByDeptId(dept_id);
		return staffs;
	}

}
